﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GDCal.vo;
using System.Collections;
using GDCal.dao;
using System.Globalization;

namespace GDCal
{
    public partial class LandPriceManagementForm : GDBaseForm
    {
        private Boolean forArea;
        public LandPriceManagementForm(Boolean forArea)
        {
            this.forArea = forArea;
            InitializeComponent();
            if (forArea)
            {
                this.streetTypeCol.Text = "Loại Vùng";
            }

            //Load all data and set to list view
            if (forArea)
            {
                fetchDataToListView(new AreaPriceVO());
            }
            else
            {
                fetchDataToListView(new LandPriceVO());
            }
            this.delete.Enabled = false;
            this.yearFilterCheckbox.Checked = false;
            this.cityCheckbox.Checked = false;

            Int32 currentYear = DateTime.Now.Year;
            this.filterYearUpDown.Minimum = currentYear - 10;
            this.filterYearUpDown.Maximum = currentYear + 10;
            this.filterYearUpDown.Value = currentYear;
            this.filterYearUpDown.Enabled = false;

            foreach (String city in this.getCities())
            {
                this.cityCombo.Items.Add(city);
            }
            this.cityCombo.SelectedIndex = 0;
            this.cityCombo.Enabled = false;
        }

        private void fetchDataToListView(LandPriceVO filter)
        {
            ArrayList datas = LandPriceDao.filter(filter);
            ListViewItem[] ret = new ListViewItem[datas.Count];

            int i = 0;
            foreach (LandPriceVO data in datas)
            {
                StreetTypeVO streetTypeVO = StreetTypeDao.getStreetType(data.getStreetTypeId());
                ListViewItem listViewItem = new ListViewItem(new string[] { Convert.ToString(data.getId()), streetTypeVO.getType(), 
                    Convert.ToString(data.getYear()), data.getPrice().ToString("N", CultureInfo.CurrentCulture), data.getCity(), data.getNote() }, -1);
                ret[i++] = listViewItem;
            }

            this.listView.Items.Clear();
            this.listView.Items.AddRange(ret);
        }

        private void fetchDataToListView(AreaPriceVO filter)
        {
            ArrayList datas = AreaPriceDao.filter(filter);
            ListViewItem[] ret = new ListViewItem[datas.Count];

            int i = 0;
            foreach (AreaPriceVO data in datas)
            {
                AreaTypeVO areaTypeVO = AreaTypeDao.getAreaType(data.getAreaTypeId());
                ListViewItem listViewItem = new ListViewItem(new string[] { Convert.ToString(data.getId()), areaTypeVO.getType(), 
                    Convert.ToString(data.getYear()), data.getPrice().ToString("N", CultureInfo.CurrentCulture), data.getCity(), data.getNote() }, -1);
                ret[i++] = listViewItem;
            }

            this.listView.Items.Clear();
            this.listView.Items.AddRange(ret);
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void addNew_Click(object sender, EventArgs e)
        {
            if (forArea)
            {
                LandPriceDetailForm landPriceDetailForm = new LandPriceDetailForm(new AreaPriceVO());
                landPriceDetailForm.ShowDialog();

                if (landPriceDetailForm.getAreaPriceVO().getId() > 0)
                {
                    doFilter();
                }
            }
            else
            {
                LandPriceDetailForm landPriceDetailForm = new LandPriceDetailForm(new LandPriceVO());
                landPriceDetailForm.ShowDialog();

                if (landPriceDetailForm.getLandPriceVO().getId() > 0)
                {
                    doFilter();
                }
            }

            if (this.listView.SelectedItems.Count == 0)
                this.delete.Enabled = false;
        }

        private void listView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listView.SelectedItems.Count == 0)
                this.delete.Enabled = false;
            else
                this.delete.Enabled = true;
        }

        private void delete_Click(object sender, EventArgs e)
        {
            if (this.listView.SelectedItems.Count > 0)
            {
                ListViewItem selectedItem = this.listView.SelectedItems[0];
                LandPriceDao.delLandPrice(Convert.ToInt32(selectedItem.SubItems[0].Text));
                doFilter();
            }
        }

        private void listView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.listView.SelectedItems.Count > 0)
            {
                ListViewItem selectedItem = this.listView.SelectedItems[0];
                if (forArea)
                {
                    AreaPriceVO selectedVO = AreaPriceDao.getAreaPrice(Convert.ToInt32(selectedItem.SubItems[0].Text));
                    LandPriceDetailForm landPriceDetailForm = new LandPriceDetailForm(selectedVO);
                    landPriceDetailForm.ShowDialog();

                    if (landPriceDetailForm.getAreaPriceVO().getId() > 0)
                    {
                        doFilter();
                    }
                }
                else
                {
                    LandPriceVO selectedVO = LandPriceDao.getLandPrice(Convert.ToInt32(selectedItem.SubItems[0].Text));
                    LandPriceDetailForm landPriceDetailForm = new LandPriceDetailForm(selectedVO);
                    landPriceDetailForm.ShowDialog();

                    if (landPriceDetailForm.getLandPriceVO().getId() > 0)
                    {
                        doFilter();
                    }
                }
                if (this.listView.SelectedItems.Count == 0)
                    this.delete.Enabled = false;
            }
        }

        private void yearFilterCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            doFilter();
        }

        private void filterYearUpDown_ValueChanged(object sender, EventArgs e)
        {
            if (this.yearFilterCheckbox.Checked)
            {
                doFilter();
            }
        }

        private void cityCheckbox_CheckedChanged(object sender, EventArgs e)
        {
            doFilter();
        }

        private void doFilter() 
        {
            if (forArea)
            {
                AreaPriceVO filter = new AreaPriceVO();
                if (this.yearFilterCheckbox.Checked)
                {
                    filter.setYear(Convert.ToInt32(filterYearUpDown.Value));
                    this.filterYearUpDown.Enabled = true;
                }
                else
                {
                    this.filterYearUpDown.Enabled = false;
                }

                if (this.cityCheckbox.Checked)
                {
                    filter.setCity((String)this.cityCombo.Items[this.cityCombo.SelectedIndex]);
                    this.cityCombo.Enabled = true;
                }
                else
                {
                    this.cityCombo.Enabled = false;
                }
                fetchDataToListView(filter);
            }
            else
            {
                LandPriceVO filter = new LandPriceVO();
                if (this.yearFilterCheckbox.Checked)
                {
                    filter.setYear(Convert.ToInt32(filterYearUpDown.Value));
                    this.filterYearUpDown.Enabled = true;
                }
                else
                {
                    this.filterYearUpDown.Enabled = false;
                }

                if (this.cityCheckbox.Checked)
                {
                    filter.setCity((String)this.cityCombo.Items[this.cityCombo.SelectedIndex]);
                    this.cityCombo.Enabled = true;
                }
                else
                {
                    this.cityCombo.Enabled = false;
                }
                fetchDataToListView(filter);
            }
        }

        private void cityCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cityCheckbox.Checked)
            {
                doFilter();
            }
        }
    }
}
